<?php
/* ******************************
 *	Recipie Production List aJax actions
 * ******************************
 */
 
include('../../../includes/classes/core.php');
$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $prod_id	= $_REQUEST['id'];
		$page		= GetPage(GetProduction($prod_id));
		
		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $local_id	= $_REQUEST['local_id'];
	    
	    $rResult = mysql_query("SELECT `TDET`.`id`,
									   `PROD`.`name`,
								       `TDET`.`quantity`
						        FROM `transfer_detail` AS `TDET` LEFT JOIN `production` AS `PROD`
						      	  ON `TDET`.`production_id` = `PROD`.`id`
						        WHERE `TDET`.`transfer_id` = $local_id");
	    
		$data = array(
			"aaData"	=> array()
		);

		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_prod':
		$comp_id		= $_REQUEST['id'];
		$local_id		= $_REQUEST['l_id'];
		
    	$prod_name		= $_REQUEST['tn'];
		$quantity 		= $_REQUEST['tq'];
		$source_object	= $_REQUEST['so'];
		
		$prod_id		= GetProductID($prod_name);
		$quantity_c		= CheckQuantity($prod_id, $source_object, $quantity);
		
		if($quantity_c){			
			if($comp_id == ''){
				if(!CheckProductionExist($local_id, $prod_id)){
					AddProduction($local_id, $prod_id, $quantity);
				}else{
					$error = '"' . $prod_name . '" უკვე არის სიაში!';
				}
			}else{
				SaveProduction($comp_id, $prod_id, $quantity);
			}
		}else{
			$error = 'არასაკმარისი პროდუქტი მარაგში!';
		}
		
        break;
    case 'disable':
		$prod_id = $_REQUEST['id'];
		DeleteProduction($prod_id);
		
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Recipie Production List Functions
 * ******************************
 */

function AddProduction($local_id, $prod_id, $quantity)
{
	mysql_query("INSERT INTO `transfer_detail`
					(`transfer_id`, `production_id`, `quantity`) 
				 VALUES
					($local_id, $prod_id, $quantity)");
}

function SaveProduction($comp_id, $prod_id, $quantity) 
{
	mysql_query("UPDATE
	    			`transfer_detail`
				 SET
					`production_id`	= $prod_id,
					`quantity`		= $quantity
				 WHERE
					`id` = $comp_id");
}

function CheckQuantity($prod_id, $obj_id, $req_quantity)
{
	$res = mysql_query("SELECT SUM(`quantity` - `sold`) AS `quantity`
						FROM `reserve`
						WHERE `production_id` = $prod_id && `object_id` = $obj_id");
	$row = mysql_fetch_assoc($res);
	if($row['quantity'] >= $req_quantity){
		return true;
	}
	return false;
}

function DeleteProduction($prod_id)
{
    mysql_query("DELETE 
				 FROM
	    			`transfer_detail`
				 WHERE
					`id` = $prod_id");
}

function CheckProductionExist($local_id, $prod_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM `transfer_detail`
										  WHERE `transfer_id` = $local_id && `production_id` = $prod_id"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}

function GetProductID($prod_name) 
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT 	`id`
										  FROM 		`production`
										  WHERE 	`name` = '$prod_name'"));
	return $res['id'];
}

function GetProduction($prod_id)
{	
    $res = mysql_fetch_assoc(mysql_query("SELECT `TDET`.`id`,
												 `PROD`.`name`,
												 `TDET`.`quantity`
										  FROM `transfer_detail` AS `TDET` LEFT JOIN `production` AS `PROD`
											ON `TDET`.`production_id` = `PROD`.`id`
										  WHERE `TDET`.`id` = $prod_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>		    
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="trans_production">პროდუქტი</label></td>
					<td>
						<div class="seoy-row" id="production_name_seoy">
							<input type="text" id="trans_production" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button class="combobox">trans_production</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="trans_quantity">რაოდენობა</label></td>
					<td>							    
						<input type="text" id="trans_quantity" onkeypress="{if (event.which != 8 &amp;&amp; event.which != 0 &amp;&amp; event.which!=46 &amp;&amp; (event.which < 48 || event.which > 57)) {$(\'#errmsg\').html(\'მხოლოდ რიცხვი\').show().fadeOut(\'slow\'); return false;}}" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					    <span id="errmsg" style="color: red; display: none;">მხოლოდ რიცხვი</span>
				    </td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="production_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>